using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WP.Commons;
using System.Reflection;
using System.Runtime.InteropServices;
namespace WoodproOfficeAddin
{
    public partial class frmOutLookAddinMain : Form
    {
        string mstrMenuName = "";
        private System.Windows.Forms.Form m_frmLastSame;
        public frmOutLookAddinMain()
        {
            InitializeComponent();
        }
        public frmOutLookAddinMain(string strMenuName)
        {
            mstrMenuName = strMenuName;
            InitializeComponent();
            BuildMenuTree();
            lblCompanyName.Text = SysVar.ReportTitle;
            this.statusStrip.Items[1].Text = SysVar.UserCompany + "(" + SysVar.DatabaseName + ") : " + SysVar.UserName;
            string strCompanNameColor = DataFunctions.GetFieldValue("mUser", "CompanyNameColor", "username='" + SysVar.UserName + "'");
            strCompanNameColor = (strCompanNameColor == string.Empty ? "0" : strCompanNameColor);
            this.statusStrip.Items[1].ForeColor = System.Drawing.Color.FromArgb(int.Parse(strCompanNameColor, System.Globalization.NumberStyles.AllowHexSpecifier));
            this.statusStrip.BackColor = SysVar.CurrrentColorTable.StatusStripGradientEnd;

        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void BuildMenuTree()
        {
            DataView dvHdr=null ,dvDtl=null ;
            if (!(new WoodproOfficeAddin.OfficeAddin()).GetWoodproOutLookMenu(out dvHdr, out dvDtl)) return;
            dvDtl.RowFilter = "OutlookMenuName='" + mstrMenuName +"'";
            tvwMenu.Nodes.Clear();
            TreeNode nodx = null;
            this.tvwMenu.Visible = true;
            foreach (DataRowView drw in dvDtl)
            {
                nodx = new TreeNode(drw["MenuName"].ToString().Trim());
                nodx.Tag = "Woodpro_" + mstrMenuName.TrimEnd().Replace(" ", "").ToUpper() + "_" + drw["MenuID"].ToString().TrimEnd();
                nodx.Name = drw["MenuID"].ToString().Trim();
                tvwMenu.Nodes.Add(nodx);
            }
            foreach (TreeNode node in tvwMenu.Nodes)
            {
                SetImageForTreeNode(node);
            }
        }

        private void SetImageForTreeNode(TreeNode trNode)
        {
            if (trNode.GetNodeCount(true) != 0)
            {
                trNode.ImageIndex = 0;
                trNode.SelectedImageIndex = 0;
                foreach (TreeNode node in trNode.Nodes)
                {
                    SetImageForTreeNode(node);
                }
            }
            else
            {
                trNode.ImageIndex = 1;
                trNode.SelectedImageIndex = 1;
            }
        }

        private void tvwMenu_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeNode nodx = e.Node;
            string strMenuTag = nodx.Tag.ToString().TrimEnd();

            string[] strMenuInfo = strMenuTag.Split(new char[] { '_' });
            if (strMenuInfo.Length == 3)
            {
                MenuInfo mInfo = DataFunctions.GetMenuInfo(strMenuInfo[2]);
                ShowForm(mInfo);
            }
        }


        public void ShowForm(MenuInfo miMenuInfo)
        {

            this.Show(); 
            
            this.Activate();
            if (CheckMaxOpenWindowsNum(miMenuInfo.strMenuId, miMenuInfo.strProgname, miMenuInfo.iParameter, miMenuInfo.strTableName))
            {
                this.m_frmLastSame.Focus();
                return;
            }
            ReflectMethod.ShowForm(miMenuInfo, FormStatus.Delete, -1, false, this);
            Application.DoEvents();


        }

        private bool CheckMaxOpenWindowsNum(string MenuID, string ProgName, int iParameter, string strTableName)
        {
            bool bIsMaxNum = false;
            int iOpenNum = 0;
            int iAllowOpenMaxNum = 0;


            for (int i = 0; i < this.MdiChildren.Length; i++)
            {
                WP.Commons.MenuInfo menuInfo = new WP.Commons.MenuInfo();
                System.Type CommonType = this.MdiChildren[i].GetType();
                PropertyInfo pInfo = CommonType.GetProperty("CurrentMenuInfo");
                if (pInfo != null)
                {
                    menuInfo = (WP.Commons.MenuInfo)pInfo.GetValue(this.MdiChildren[i], null);
                }
                if (menuInfo.strProgname != null)
                {
                    if (menuInfo.strProgname != "" && menuInfo.strProgname.Trim().ToUpper() == ProgName.ToUpper().Trim() && menuInfo.iParameter == iParameter)
                    {
                        if (menuInfo.strTableName.Trim().ToUpper() == strTableName.ToUpper().Trim())
                        {
                            iOpenNum += 1;
                            m_frmLastSame = this.MdiChildren[i];
                        }
                    }
                }
                else
                {
                    if (this.MdiChildren[i].Name.Trim().ToUpper() == ProgName.ToUpper().Trim())
                    {
                        iOpenNum += 1;
                        m_frmLastSame = this.MdiChildren[i];
                    }
                }

            }


            iAllowOpenMaxNum = CvtFunc.ToInt(DataFunctions.GetFieldValue("mMenu", "MaxOpenWindowsNum", "MenuID ='" + MenuID + "'"));
            if (iOpenNum >= iAllowOpenMaxNum)
                bIsMaxNum = true;

            return bIsMaxNum;
        }

        private void mdiChildFormTabStrip_MdiTabAdded(object sender, MdiTabStrip.MdiTabStripTabEventArgs e)
        {
            mdiChildFormTabStrip.Visible = mdiChildFormTabStrip.Tabs.Count > 0;
        }

        private void mdiChildFormTabStrip_MdiTabRemoved(object sender, MdiTabStrip.MdiTabStripTabEventArgs e)
        {
            mdiChildFormTabStrip.Visible = mdiChildFormTabStrip.Tabs.Count > 0;
        }
    }
}